<template>
  <div class="dashboard-layout">
    <!-- 顶部导航栏 -->
    <div class="dashboard-header">
      <div class="header-left">
        <div class="logo">
          <span class="logo-icon">{{ logoIcon }}</span>
          <span class="logo-text">{{ title }}</span>
        </div>
      </div>
      <div class="header-right">
        <el-dropdown @command="handleDropdownCommand" trigger="click">
          <div class="user-dropdown-trigger">
            <el-icon class="user-icon"><UserFilled /></el-icon>
            <div class="user-info">
              <span class="user-name">{{ username }}</span>
              <el-icon class="dropdown-arrow"><ArrowDown /></el-icon>
            </div>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile" v-if="userType === 'user'">
                <el-icon><User /></el-icon>
                <span>个人信息</span>
              </el-dropdown-item>
              <el-dropdown-item command="logout" divided>
                <el-icon><SwitchButton /></el-icon>
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <div class="dashboard-container">
      <!-- 侧边栏 -->
      <div class="dashboard-sidebar">
        <el-menu 
          :default-active="activeMenu" 
          @select="handleMenuSelect" 
          mode="vertical"
          class="dashboard-menu"
        >
          <slot name="menu"></slot>
        </el-menu>
      </div>
      
      <!-- 主内容区 -->
      <div class="dashboard-main">
        <!-- 面包屑导航 -->
        <div class="breadcrumb-container" v-if="showBreadcrumb">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item>{{ breadcrumbHome }}</el-breadcrumb-item>
            <el-breadcrumb-item>{{ currentTitle }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>

        <!-- 内容区域 -->
        <div class="content-wrapper">
          <slot name="content"></slot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { UserFilled, ArrowDown, SwitchButton, User } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

export default {
  name: 'DashboardLayout',
  components: {
    UserFilled,
    ArrowDown,
    SwitchButton,
    User
  },
  props: {
    title: {
      type: String,
      default: '后台管理'
    },
    logoIcon: {
      type: String,
      default: '🍽️'
    },
    username: {
      type: String,
      default: '用户'
    },
    userType: {
      type: String,
      default: 'user'
    },
    activeMenu: {
      type: String,
      default: 'overview'
    },
    currentTitle: {
      type: String,
      default: '概览'
    },
    showBreadcrumb: {
      type: Boolean,
      default: true
    },
    breadcrumbHome: {
      type: String,
      default: '管理后台'
    }
  },
  emits: ['menu-select', 'logout', 'profile'],
  methods: {
    handleMenuSelect(index) {
      this.$emit('menu-select', index)
    },
    handleDropdownCommand(command) {
      if (command === 'logout') {
        this.logout()
      } else if (command === 'profile') {
        this.$emit('profile')
      }
    },
    logout() {
      ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$emit('logout')
      }).catch(() => {
        // 用户取消退出
      })
    }
  }
}
</script>

<style scoped>
.dashboard-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f5f7fa;
}

/* 顶部导航栏 */
.dashboard-header {
  height: 64px;
  background: #ffffff;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  z-index: 1000;
}

.header-left .logo {
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: 600;
  color: #1f2937;
}

.logo-icon {
  font-size: 24px;
  margin-right: 8px;
}

.logo-text {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-dropdown-trigger {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.user-dropdown-trigger:hover {
  background: #f8fafc;
}

.user-icon {
  font-size: 20px;
  color: #6b7280;
  margin-right: 8px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-name {
  font-size: 14px;
  font-weight: 500;
  color: #374151;
  margin-right: 6px;
}

.dropdown-arrow {
  font-size: 12px;
  color: #9ca3af;
  transition: transform 0.3s ease;
}

/* 主容器 */
.dashboard-container {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* 侧边栏 */
.dashboard-sidebar {
  width: 240px;
  background: #ffffff;
  border-right: 1px solid #e4e7ed;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.06);
}

.dashboard-menu {
  border-right: none;
  height: 100%;
}

.dashboard-menu .el-menu-item {
  height: 56px;
  line-height: 56px;
  margin: 4px 12px;
  border-radius: 8px;
  color: #6b7280;
  font-weight: 500;
  transition: all 0.3s ease;
}

.dashboard-menu .el-menu-item:hover {
  background: #f1f5f9;
  color: #3b82f6;
}

.dashboard-menu .el-menu-item.is-active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #ffffff;
  font-weight: 600;
}

.dashboard-menu .el-menu-item .el-icon {
  margin-right: 12px;
  font-size: 16px;
}

/* 主内容区 */
.dashboard-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.breadcrumb-container {
  background: #ffffff;
  padding: 16px 24px;
  border-bottom: 1px solid #e4e7ed;
}

.breadcrumb-container .el-breadcrumb {
  font-size: 14px;
}

.breadcrumb-container .el-breadcrumb__inner {
  color: #6b7280;
  font-weight: 500;
}

.breadcrumb-container .el-breadcrumb__inner.is-link {
  color: #3b82f6;
}

.content-wrapper {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  background: #f8fafc;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .dashboard-sidebar {
    width: 200px;
  }
  
  .logo-text {
    display: none;
  }
  
  .user-name {
    display: none;
  }
  
  .content-wrapper {
    padding: 16px;
  }
}

@media (max-width: 480px) {
  .dashboard-sidebar {
    width: 60px;
  }
  
  .dashboard-menu .el-menu-item span {
    display: none;
  }
  
  .dashboard-header {
    padding: 0 16px;
  }
}
</style> 